﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using KingRoad.EPM.PersonnelLogEntity.Model;
using Unis.Framework.Core.Utility;
using KingRoad.EPM.PersonnelLogDAL.PlmPersonnelinfoService;
using KingRoad.EPM.PersonnelLogDAL.PlmPersonnelinforecordService;


namespace KingRoad.EPM.PersonnelLogBLL.UIRules
{
    /// <summary> 
    /// 客户端业务处理类。 
    /// 文件生成时间 2017/1/12 13:58:16
    /// </summary> 
    public class PlmPersonnelinfoUIRule
    {
        private readonly IPlmPersonnelinfoService _plmPersonnelinfoService;
        private PlmPersonnelinforecordUIRule _plmpersonnelinforecordUIRule;

        public PlmPersonnelinfoUIRule() { }

        /// <summary> 
        /// construct function... 
        /// </summary> 
        public PlmPersonnelinfoUIRule(IPlmPersonnelinfoService plmPersonnelinfoService)
        {
            _plmPersonnelinfoService = plmPersonnelinfoService;
        }

        /// <summary>
        /// 获取人员信息
        /// </summary>
        /// <returns></returns>
        public List<PlmPersonnelinfoInfo> GetAllPersonnelinfo(string condition = " 1=1 ")
        {
            var list = _plmPersonnelinfoService.GetEntityList(condition);
            return list;
        }
        /// <summary>
        /// 根据条件查询费用计划信息
        /// </summary>
        /// <param name="numCurrentPage">当前页</param>
        /// <param name="numPerPage">每页数据大小</param>
        /// <param name="orderByField">排序字段名称</param>
        /// <param name="totalRecord">总数据量</param>
        /// <param name="sqlCondition">sql查询条件</param>
        /// <returns></returns>
        public List<PlmPersonnelinfoInfo> GetAllCostplanmainForPage(int numCurrentPage, int numPerPage,
                                                                        string orderByField, out int totalRecord, string sqlCondition)
        {
            var list = _plmPersonnelinfoService.GetEntityListByContionRe(numCurrentPage, numPerPage, orderByField, out totalRecord, sqlCondition, "asc");
            return list;
        }
        /// <summary>
        /// 获取人员信息 根据ID获取
        /// </summary>
        /// <returns></returns>
        public PlmPersonnelinfoInfo GetPersonnelinfoByID(Guid id)
        {
            var list = _plmPersonnelinfoService.GetEntityById(id);
            return list;
        }
        public List<PlmPersonnelinfoInfo> GetListBySql(string name, string section)
        {
            var list = _plmPersonnelinfoService.GetEntityList("replace(Pi_Name,' ','')=replace('"+name+"',' ','') and Pi_SectionName='" + section + "'");
            return list;
        }
        /// <summary>
        /// 根据标段名称 获取该标段人员信息
        /// </summary>
        /// <param name="section"></param>
        /// <returns></returns>
        public List<PlmPersonnelinfoInfo> GetPersonlInfoBySectionName(string section)
        {
            var list = _plmPersonnelinfoService.GetEntityList("Pi_SectionName='" + section + "'");
            return list;
        }

        /// <summary>
        /// 添加人员信息
        /// </summary>
        /// <returns></returns>
        public int AddPersonnelinfo(PlmPersonnelinfoInfo AddEntity)
        {
            _plmpersonnelinforecordUIRule = new PlmPersonnelinforecordUIRule(new PlmPersonnelinforecordService());
            PlmPersonnelinforecordInfo pir = new PlmPersonnelinforecordInfo();
            pir.Pi_ID = AddEntity.Pi_ID;
            pir.Pi_SectionID = AddEntity.Pi_SectionID;
            pir.Pi_SectionName = AddEntity.Pi_SectionName;
            pir.Pi_UserID = AddEntity.Pi_UserID;
            pir.Pi_UserName = AddEntity.Pi_UserName;
            pir.Pi_UpdateTime = AddEntity.Pi_UpdateTime;
            pir.Pi_UnitID = AddEntity.Pi_UnitID;
            pir.Pi_UnittName = AddEntity.Pi_UnittName;
            pir.Pi_Name = AddEntity.Pi_Name;
            pir.Pi_Sex = AddEntity.Pi_Sex;
            pir.Pi_Age = AddEntity.Pi_Age;
            pir.Pi_IDNo = AddEntity.Pi_IDNo;
            pir.Pi_Post = AddEntity.Pi_Post;
            pir.Pi_Professional = AddEntity.Pi_Professional;
            pir.Pi_Phone = AddEntity.Pi_Phone;
            pir.Pi_Email = AddEntity.Pi_Email;
            pir.Pi_License = AddEntity.Pi_License;
            pir.Pi_LicenseNum = AddEntity.Pi_LicenseNum;
            pir.Pi_WorkNum = AddEntity.Pi_WorkNum;
            pir.Pi_InTime = DateTime.Now;

            int saferecord = _plmpersonnelinforecordUIRule.AddPersonnelinfo(pir);
            return _plmPersonnelinfoService.InsertEntity(AddEntity);
        }

        /// <summary>
        /// 删除人员信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeletePersonnelinfo(string id)
        {
            return _plmPersonnelinfoService.DeleteEntityByStringID(id);
        }

        /// <summary>
        /// 删除人员信息(批量删除)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeletePersonnelinfoMulti(string id)
        {
            return _plmPersonnelinfoService.DeleteEntityByMultiStringID(id);
        }

        /// <summary>
        /// 更新人员信息
        /// </summary>
        /// <returns></returns>
        public int UpdatePersonnelinfo(PlmPersonnelinfoInfo UpdateEntity)
        {
            _plmpersonnelinforecordUIRule = new PlmPersonnelinforecordUIRule(new PlmPersonnelinforecordService());
            var pir = _plmpersonnelinforecordUIRule.GetPersonnelinfoByID(Guid.Parse(UpdateEntity.Pi_ID));
            if (pir != null)
            {
                pir.Pi_SectionID = UpdateEntity.Pi_SectionID;
                pir.Pi_SectionName = UpdateEntity.Pi_SectionName;
                pir.Pi_UserID = UpdateEntity.Pi_UserID;
                pir.Pi_UserName = UpdateEntity.Pi_UserName;
                pir.Pi_UpdateTime = UpdateEntity.Pi_UpdateTime;
                pir.Pi_UnitID = UpdateEntity.Pi_UnitID;
                pir.Pi_UnittName = UpdateEntity.Pi_UnittName;
                pir.Pi_Name = UpdateEntity.Pi_Name;
                pir.Pi_Sex = UpdateEntity.Pi_Sex;
                pir.Pi_Age = UpdateEntity.Pi_Age;
                pir.Pi_IDNo = UpdateEntity.Pi_IDNo;
                pir.Pi_Post = UpdateEntity.Pi_Post;
                pir.Pi_Professional = UpdateEntity.Pi_Professional;
                pir.Pi_Phone = UpdateEntity.Pi_Phone;
                pir.Pi_Email = UpdateEntity.Pi_Email;
                pir.Pi_License = UpdateEntity.Pi_License;
                pir.Pi_LicenseNum = UpdateEntity.Pi_LicenseNum;
                pir.Pi_WorkNum = UpdateEntity.Pi_WorkNum;
                int saferecord = _plmpersonnelinforecordUIRule.UpdatePersonnelinfo(pir);
            }
            return _plmPersonnelinfoService.UpdateEntity(UpdateEntity);
        }

        /// <summary>
        /// 获取所有的人员信息
        /// </summary>
        /// <param name="numCurrentPage">当前页</param>
        /// <param name="numPerPage">每页数据大小</param>
        /// <param name="orderByField">排序字段名称</param>
        /// <param name="totalRecord">总数据量</param>
        /// <returns></returns>
        public List<PlmPersonnelinfoInfo> GetAllPersonnelinfo(int numCurrentPage, int numPerPage, string orderByField, out int totalRecord)
        {
            var list = _plmPersonnelinfoService.GetEntityListByContion(numCurrentPage, numPerPage, orderByField, out totalRecord);
            return list;
        }

        /// <summary>
        /// 根据条件查询人员信息
        /// </summary>
        /// <param name="numCurrentPage">当前页</param>
        /// <param name="numPerPage">每页数据大小</param>
        /// <param name="orderByField">排序字段名称</param>
        /// <param name="totalRecord">总数据量</param>
        /// <param name="sqlCondition">sql查询条件</param>
        /// <returns></returns>
        public List<PlmPersonnelinfoInfo> GetPersonnelinfoForSearch(int numCurrentPage, int numPerPage,
                                                                        string orderByField, out int totalRecord, string sqlCondition)
        {
            var list = _plmPersonnelinfoService.GetEntityListByContion(numCurrentPage, numPerPage, orderByField, out totalRecord, sqlCondition);
            return list;
        }

        /// <summary>
        /// 判断是否存在AssisId
        /// </summary>
        /// <param name="assisId"></param>
        /// <returns></returns>
        public bool IsExitAssistId(int assisId)
        {
            var strSql = String.Format(" Assist_ID={0} ", assisId);
            var list = _plmPersonnelinfoService.GetEntityList(strSql);
            return list.Any();
        }
    }
}
